package com.qf.web.controller;

import com.qf.domain.*;
import com.qf.service.AddressService;
import com.qf.service.CartService;
import com.qf.service.OrderService;
import com.qf.service.impl.AddressServiceImpl;
import com.qf.service.impl.CartServiceImpl;
import com.qf.service.impl.OrderServiceImpl;
import com.qf.utils.RandomUtils;
import com.qf.utils.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * projectName:day15_myxiaomi
 * author:lcy
 * time:2023/11/26 10:11
 * description:
 */
@WebServlet("/orderservlet")
public class OrderServlet extends BaseServlet {
    //orderservlet?method=addOrder&aid=7
    public String addOrder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        User user = (User) request.getSession().getAttribute("user");
        if (user == null) {
            return "redirect:/login.jsp";
        }
        String aid = request.getParameter("aid");
        if (StringUtils.isEmpty(aid)) {
            request.setAttribute("msg", "地址不能为空");
            return "message.jsp";
        }
        OrderService orderService = new OrderServiceImpl();
        CartService cartService = new CartServiceImpl();
        List<Cart> cartList = cartService.find(user.getId());
        if (cartList == null || cartList.size() == 0) {
            request.setAttribute("msg", "购物车不能为空");
            return "message.jsp";
        }
        BigDecimal money = new BigDecimal(0);
        for (Cart cart : cartList) {
            money = money.add(cart.getMoney());
        }
        try {
            String oid = RandomUtils.CreteorderId();
            Order order = new Order(oid, user.getId(), money, "1", new Date(), Integer.parseInt(aid));
            orderService.submitOrder(order, cartList);
            request.setAttribute("order", order);
            return "/";
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("msg", "添加订单失败：" + e.getMessage());
            return "message.jsp";
        }
    }

    //orderservlet?method=getOrderView
    public String getOrderView(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        User user = (User) request.getSession().getAttribute("user");
        if (user == null) {
            return "redirect:/login.jsp";
        }
        //OrderService orderService=new OrderServiceImpl();
        try {
            CartService cartService = new CartServiceImpl();
            List<Cart> cartList = cartService.find(user.getId());
            AddressService addressService = new AddressServiceImpl();
            List<Address> addList = addressService.find(user.getId());
            request.setAttribute("cartList", cartList);
            request.setAttribute("addList", addList);
            return "order.jsp";
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("msg", "预览对象失败：" + e.getMessage());
            return "message.jsp";
        }
    }

    //orderservlet?method=getOrderList
    public String getOrderList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        User user = (User) request.getSession().getAttribute("user");
        if (user == null) {
            return "redirect:/login.jsp";
        }
        try {
            OrderService orderService = new OrderServiceImpl();
            List<Order> orderList = orderService.find(user.getId());
            request.setAttribute("orderList", orderList);
            return "orderList.jsp";
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("msg", "查询订单失败：" + e.getMessage());
            return "message.jsp";
        }
    }

    // orderservlet?method=getOrderDetail&oid
    public String getOrderDetail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        User user = (User) request.getSession().getAttribute("user");
        if (user == null) {
            return "redirect:/login.jsp";
        }
        String oid = request.getParameter("oid");
        if (StringUtils.isEmpty(oid)) {
            request.setAttribute("msg", "订单编号不能为空");
            return "message.jsp";
        }
        try {
            OrderService orderService = new OrderServiceImpl();
            Order order = orderService.findByOid(oid);
            List<OrderDetail> orderDetail = orderService.findDetail(oid);
            request.setAttribute("order", order);
            request.setAttribute("orderDetail", orderDetail);
            return "orderDetail.jsp";
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("msg", "查询订单详情失败" + e.getMessage());
            return "message.jsp";
        }
    }

    //orderservlet?method=searchOrder&username="+username+"&status="+status;
    //orderservlet?method=sendOrder?oid="+id;
    public String searchOrder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        String username = request.getParameter("username");
        String status = request.getParameter("status");
        //String oid = request.getParameter("oid");
        String id = request.getParameter("id");
        System.out.println("username="+username);
        System.out.println("id="+id);
        System.out.println("status="+status);

        OrderService orderService=new OrderServiceImpl();
        List<Object> params =new ArrayList<>();
        StringBuilder where =new StringBuilder(" where 1=1 ");
        //User user=new User();
        if(!StringUtils.isEmpty(id)){
            where.append(" and id=? ");
            params.add(id);
        }
        if(!StringUtils.isEmpty(status)){
            where.append(" and status=? ");
            params.add(status);
        }
        try {
            //List<Order> orderList = orderService.findAll();
            List<Order> orderList = orderService.fAll(where.toString(),params);
            request.setAttribute("orderList",orderList);
            return  "admin/showAllOrder.jsp";
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("msg", "查看管理失败："+e.getMessage());
            return "admin/message.jsp";
        }
    }
}